var t = require("../../../@babel/runtime/helpers/typeof"), o = getApp();

Page({
    data: {
        goodsPopupShow: !1,
        themeColor: "#f14e4e",
        store_id: "",
        goodsId: "",
        cancelClick: !1,
        addShow: !1
    },
    onLoad: function(t) {
        var a = this;
        if (t.scene) {
            var s = decodeURIComponent(t.scene);
            if (s) {
                var e = [];
                e = s.split(",");
                for (var i = 0; i < e.length; i++) {
                    var r;
                    "mid" == (r = e[i].split("="))[0] && o._setMid(r[1]), "id" == r[0] && (this.setData({
                        goodsId: r[1]
                    }), this.getGoodsDetail(this.data.goodsId)), "store_id" == r[0] && (this.setData({
                        store_id: r[1]
                    }), this.getStoreInfo(), this.getCart());
                }
            }
        }
        t.store_id && (this.setData({
            store_id: t.store_id
        }), this.getStoreInfo(), this.getCart()), t.id && (this.setData({
            goodsId: t.id
        }), this.getGoodsDetail(this.data.goodsId)), wx.setNavigationBarTitle({
            title: "自助买单"
        }), wx.getStorage({
            key: "yz_basic_info",
            success: function(t) {
                a.setData({
                    themeColor: t.data.theme_color
                });
            }
        });
    },
    toOrderChild: function() {
        this.data.goodsCarts.length <= 0 ? o.tips("购物车暂无商品") : (this.setData({
            goodsPopupShow: !1,
            goodsId: ""
        }), this.goodsOrder());
    },
    closeShow: function() {
        this.setData({
            goodsPopupShow: !1,
            goodsId: ""
        });
    },
    continueCode: function() {
        this.closeShowAdd(), this.scanCode();
    },
    closeShowAdd: function() {
        this.setData({
            goodsPopupShow: !1,
            goodsId: "",
            addShow: !1
        });
    },
    addPopupClose: function() {
        this.setData({
            addShow: !1
        });
    },
    CutTap: function(t) {
        if (!this.data.cancelClick) {
            var o = t.currentTarget.dataset.good.id;
            this.updateCartRequest(o, "-1");
        }
    },
    AddTap: function(t) {
        console.log(t);
        var o = t.currentTarget.dataset.good, a = t.currentTarget.dataset.tag;
        this.addCart(o, a);
    },
    updateCartRequest: function(t, a, s) {
        var e = this;
        wx.showLoading({
            title: "加载中"
        }), console.log(t, a);
        var i = o.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.updateNum");
        i += "&id=" + t, i += "&num=" + a, i += "&store_id=" + this.data.store_id, o._getNetWork({
            url: i,
            success: function(t) {
                var o = t.data;
                wx.hideLoading(), 1 == o.result ? (a < 0 ? e.setData({
                    popNum: e.data.popNum - 1
                }) : e.setData({
                    popNum: e.data.popNum + 1
                }), "goodPopup" == s && e.setData({
                    addShow: !0
                }), e.getCart(), e.data.cancelClick = !1) : (wx.showToast({
                    title: o.msg,
                    icon: "none",
                    duration: 1500
                }), e.getCart(), e.data.cancelClick = !1);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    childAddCart: function(t) {
        var o = t.detail.goodsInfo, a = t.detail.optionsId, s = t.detail.goodsCount, e = null;
        if (a) {
            for (var i = 0; i < this.data.carts.length; i++) this.data.carts[i].option_id == a && (e = i);
            if (null == e) this.addCart(o, "goodPopup", s, a); else {
                var r = Number(s) + Number(this.data.carts[e].total) - Number(this.data.carts[e].total);
                this.updateCartRequest(this.data.carts[e].id, r, "goodPopup");
            }
        } else {
            for (var d = 0; d < this.data.carts.length; d++) this.data.carts[d].goods_id == o.id && (e = d);
            if (null == e) this.addCart(o, "goodPopup", s, a); else {
                var n = Number(s) + Number(this.data.carts[e].total) - Number(this.data.carts[e].total);
                this.updateCartRequest(this.data.carts[e].id, n, "goodPopup");
            }
        }
    },
    changeNum: function(t) {
        var o = t.currentTarget.dataset.total, a = t.detail.value, s = t.currentTarget.dataset.ind, e = this.data.carts[s];
        if (a < 0) return wx.showToast({
            icon: "none",
            title: "商品数量不能为负数",
            duration: 1500
        }), this.data.carts[s].total = o, void this.setData({
            carts: this.data.carts
        });
        if (a < o) {
            var i = o - a;
            this.updateCartRequest(e.id, -i);
        } else if (a > o) {
            var r = Number(a) - Number(o);
            this.updateCartRequest(e.id, r);
        }
    },
    addCart: function(t, a, s, e) {
        var i = this;
        console.log(t);
        var r = "";
        r = "cart" === a ? t.goods_id : t.id;
        var d = o.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.store");
        d += "&goods_id=" + r, "cart" == a ? (d += "&option_id=" + (t.option_id ? t.option_id : 0), 
        d += "&total=1") : (d += "&option_id=" + e, d += "&total=" + s), d += "&store_id=" + this.data.store_id, 
        wx.showLoading({
            title: "加载中"
        }), o._getNetWork({
            url: d,
            success: function(t) {
                var o = t.data;
                wx.hideLoading(), 1 == o.result ? ("goodPopup" == a && i.setData({
                    addShow: !0
                }), i.getCart()) : (i.getCart(), wx.showToast({
                    title: o.msg,
                    icon: "none",
                    duration: 1500
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    clearCart: function() {
        var t = this, a = [];
        if (this.data.carts.forEach(function(t) {
            a.push(t.id);
        }), !(a.length <= 0)) {
            var s = o.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.destroy");
            s += "&store_id=" + this.data.store_id, s += "&ids=" + a, o._getNetWork({
                url: s,
                success: function(o) {
                    var a = o.data;
                    1 == a.result ? (t.getCart(), t.setData({
                        popNum: 0
                    })) : wx.showToast({
                        title: a.msg,
                        icon: "none",
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    getCart: function() {
        var t = this, a = o.getNetAddresss("plugin.store-cashier.frontend.shoppingCart.member-cart.index");
        o._postNetWork({
            url: a,
            data: {
                store_id: this.data.store_id
            },
            success: function(o) {
                var a = o.data;
                if (1 == a.result) {
                    if (0 == a.result) return;
                    console.log(a.data), t.getFirstCart(a.data);
                } else wx.showToast({
                    title: a.msg
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    getStoreInfo: function() {
        var t = this, a = o.getNetAddresss("plugin.store-cashier.frontend.store.get-store-info.get-stores-details");
        o._postNetWork({
            url: a,
            data: {
                store_id: this.data.store_id
            },
            success: function(o) {
                var a = o.data;
                if (1 == a.result) {
                    if (0 == a.result) return;
                    t.setData({
                        storeInfo: a.data
                    });
                } else wx.showToast({
                    title: a.msg
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    getFirstCart: function(t) {
        var o = this;
        this.setData({
            carts: t,
            goodsCarts: [],
            cartsNum: 0,
            cartsTotal: 0
        }), console.log(t, this.data.carts);
        var a = {};
        this.data.carts.forEach(function(t) {
            a[t.goods_id] = t, o.setData({
                cartsNum: o.data.cartsNum + t.total,
                cartsTotal: o.data.cartsTotal + t.total * t.goods.price
            });
        }), this.setData({
            goodsCarts: this.data.carts.length > 0 ? a : [],
            goodsCartsTotal: this.calculateTotal(this.data.carts),
            cartsTotal: parseFloat(this.data.cartsTotal).toFixed(2)
        }), console.log(this.data.carts, this.data.goodsCarts, "处理购物车数据");
    },
    calculateTotal: function(t) {
        var o = [], a = {}, s = {};
        for (var e in t) {
            var i = t[e].goods_id;
            a[i] ? a[i].total = a[i].total + t[e].total : (a[i] = {}, a[i].goods_id = t[e].goods_id, 
            a[i].total = t[e].total);
        }
        for (var r in a) o.push(a[r]);
        return o.forEach(function(t) {
            s[t.goods_id] = t;
        }), s;
    },
    scanCode: function() {
        var o = this;
        wx.scanCode({
            success: function(a) {
                if ("WX_CODE" == a.scanType) {
                    var s = function() {
                        var t = decodeURIComponent(a.path).split("scene=")[1], s = t.split(",");
                        console.log(t, s);
                        for (var e = 0; e < s.length; e++) {
                            var i;
                            if ("id" == (i = s[e].split("="))[0] && o.setData({
                                goodsId: i[1]
                            }), "store_id" == i[0]) {
                                if (o.data.store_id && i[1] != o.data.store_id) return wx.showModal({
                                    title: "提示",
                                    content: "当前商品不属于此门店是否跳转到该门店",
                                    complete: function(t) {
                                        if (t.confirm) {
                                            for (var o = "/packageI/storePriceTag/storePriceTagIndex/storePriceTagIndex", a = 0; a < s.length; a++) o += 0 == a ? "?".concat(s[0]) : "&".concat(s[a]);
                                            wx.redirectTo({
                                                url: o
                                            });
                                        }
                                    }
                                }), {
                                    v: void 0
                                };
                                console.log("是当前门店的商品价签码"), o.setData({
                                    store_id: i[1]
                                });
                            }
                        }
                        return o.getGoodsDetail(o.data.goodsId), {
                            v: void 0
                        };
                    }();
                    if ("object" === t(s)) return s.v;
                }
                o.getGoodsDetail(a.result);
            }
        });
    },
    getGoodsDetail: function(t) {
        var a = this, s = o.getNetAddresss("plugin.store-price-tag.frontend.goods.get-goods-page");
        o._postNetWork({
            url: s,
            data: {
                id: t
            },
            success: function(o) {
                var s = o.data;
                if (1 == s.result) {
                    if (s.data.get_store_info.store_id != a.data.store_id) return void wx.showModal({
                        title: "提示",
                        content: "当前商品不属于此门店是否跳转到该门店",
                        complete: function(o) {
                            o.confirm && wx.redirectTo({
                                url: "/packageI/storePriceTag/storePriceTagIndex/storePriceTagIndex?store_id=".concat(s.data.get_store_info.store_id, "&id=").concat(t)
                            });
                        }
                    });
                    a.setData({
                        goodsInfo: s.data.get_goods,
                        goodsPopupShow: !0
                    });
                } else wx.showToast({
                    title: s.msg
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    goodsOrder: function() {
        var t = this.data.goodsCarts;
        console.log(this.data.goodsCarts), this._goodsOrder(t);
    },
    _goodsOrder: function(t, a) {
        var s = 0;
        (a || this.isHasDifferType(t)) && (this.data.carts.forEach(function(t) {
            s += t.total;
        }), 0 != s ? 0 != t.length && 0 != this.data.carts.length && wx.navigateTo({
            url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=store&store_id=" + this.data.store_id
        }) : o.tips("请添加购买的商品"));
    },
    isHasDifferType: function(t) {
        var o = this, a = 0, s = 0, e = "";
        for (var i in t) i.live_install && 1 == i.live_install.open_state ? (a += 1, e = i.live_install.plugin_name) : s += 1;
        return 0 == a || 0 == s || (wx.showModal({
            title: "提示",
            content: "普通商品与".concat(e, "商品一同下单将无法享受").concat(e, "，是否继续下单？"),
            success: function(a) {
                a.confirm ? o._goodsOrder(t, !0) : a.cancel && console.log(a.cancel);
            }
        }), !1);
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});